package com.aat.dxfy.spring;

import javax.servlet.http.HttpSession;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class BaseController {
	
	//此处单例模式获得；
	private ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext();
	
	public <T> T service(Class<T> cls){
		T t =ctx.getBean(cls);
		return t;
	}
	
	public HttpSession getSession() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return attrs.getRequest().getSession();
	}
	
	public void addSessionObj(String key,Object obj) {
		String sid=getSession().getId();
		getSession().setAttribute(sid+key, obj);
	}
	
	public <T> T getSessionObj(String key, Class<T> cls) {
		String sid=getSession().getId();
		Object obj = getSession().getAttribute(sid+key);
		if(obj!=null){
			return cls.cast(obj);
		}
		return null;
		
	}
}
